using UnityEngine;

namespace Gj
{
    public enum SimpleAnimationType
    {
        None,
        Scale,
        Position,
        Rotation
    }

    public class SimpleAnimationHelper : AnimationHelper
    {
        public SimpleAnimationType type = SimpleAnimationType.None;
        public AnimationCurve curve = AnimationCurve.Linear(0, 0, 1, 1);
        public bool init;
        public bool local = true;
        public bool autoStart;
        public bool autoEnd;
        public Vector3 start = Vector3.zero;
        public Vector3 end = Vector3.zero;

        protected override void Init()
        {
            if (autoStart || autoEnd) AutoInit();
        }

        private void AutoInit()
        {
            switch (type)
            {
                case SimpleAnimationType.Position:
                    if (autoStart)
                    {
                        start = local ? Transform.localPosition : Transform.position;
                        end += start;
                    }

                    if (autoEnd)
                    {
                        end = local ? Transform.localPosition : Transform.position;
                        start -= end;
                    }

                    break;
                case SimpleAnimationType.Rotation:
                    if (autoStart)
                    {
                        start = local ? Transform.localRotation.eulerAngles : Transform.rotation.eulerAngles;
                        end += start;
                    }

                    if (autoEnd)
                    {
                        end = local ? Transform.localRotation.eulerAngles : Transform.rotation.eulerAngles;
                        start -= end;
                    }

                    break;
                case SimpleAnimationType.Scale:
                    if (autoStart)
                    {
                        start = Transform.localScale;
                        end += start;
                    }

                    if (autoEnd)
                    {
                        end = Transform.localScale;
                        start -= end;
                    }

                    break;
            }
        }

        protected virtual Vector3 GetValue(float value)
        {
            return Vector3.Lerp(start, end,  easeType == iTween.EaseType.linear ? curve.Evaluate(value) : value);
        }

        protected override void OnUpdate(float value)
        {
            var v = GetValue(value);
            switch (type)
            {
                case SimpleAnimationType.Scale:
                    Transform.localScale = v;
                    break;
                case SimpleAnimationType.Position:
                    if (local)
                        Transform.localPosition = v;
                    else
                        Transform.position = v;
                    break;
                case SimpleAnimationType.Rotation:
                    if (local)
                        Transform.localRotation = Quaternion.Euler(v);
                    else
                        Transform.rotation = Quaternion.Euler(v);
                    break;
            }
        }

        protected override void OnStart()
        {
            if (init)
            {
                var v = forward ? start : end;
                switch (type)
                {
                    case SimpleAnimationType.Scale:
                        Transform.localScale = v;
                        break;
                    case SimpleAnimationType.Position:
                        if (local)
                            Transform.localPosition = v;
                        else
                            Transform.position = v;
                        break;
                    case SimpleAnimationType.Rotation:
                        if (local)
                            Transform.localRotation = Quaternion.Euler(v);
                        else
                            Transform.rotation = Quaternion.Euler(v);
                        break;
                }
            }
            base.OnStart();
        }
    }
}